package com.polaris.live.ui.party.widget.seat.party_seat

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.findViewTreeLifecycleOwner
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.constant.PagConst
import com.polaris.live.databinding.WidgetSeatItemBinding
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.interface_bag.PAGListener
import com.polaris.live.resp.back_resp.UserRoomThemeResp
import com.polaris.live.translation.R.string
import com.polaris.live.ui.party.widget.seat.base.BaseSeatItemView
import com.polaris.live.ui.party.widget.seat.bean.SeatBean
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.playAsyncWithOwner
import com.polaris.live.widget.avatar.FrameImageUtils
import org.libpag.PAGFile
import org.libpag.PAGImageView

/**
 * SeatItemView
 *
 * @author Created by 天晴 on 2024/2/29 15:58
 * @since 1.0.0
 **/
class SeatItemView(context: Context, attrs: AttributeSet?) :
    BaseSeatItemView(context, attrs) {

    private val mBinding = WidgetSeatItemBinding.inflate(LayoutInflater.from(context), this)

    //房主id
    private var mUserId: Long? = null

    //麦位皮肤图标
    private var seatStateSkinUrl: String? = null

    //麦位皮肤锁定图标
    private var seatLockSkinUrl: String? = null

    init {
        mBinding.pagEmoji.apply {
            setRepeatCount(1)
            addListener(object : PAGListener {
                override fun onAnimationEnd(p0: PAGImageView?) {
                    super.onAnimationEnd(p0)
                    visibility = View.GONE
                    mBinding.avRl.visibility = View.VISIBLE
                }

                override fun onAnimationStart(p0: PAGImageView?) {
                    super.onAnimationStart(p0)
                    mBinding.avRl.visibility = View.INVISIBLE
                }
            })
        }
    }

    override fun upDataView(mData: SeatBean, userId: Long?, isReset: Boolean) {
        if (userId != null) {
            mUserId = userId
        }
        val userBasic = mData.userBasicVo
        if (userBasic != null) {
            setAvatar(userBasic)
            setGainCoin(mData.siteGainCoin)
            setName(userBasic)
            setMike(mData.siteMicStatus == 1)
        } else {
            setNoUser()
            if (!isReset) {
                setLock()
            }
        }
        pausePag()
    }

    private fun setGainCoin(siteGainCoin: Long?) {
        mBinding.giftTv.text = "${siteGainCoin ?: 0}"
    }

    private fun setLock() {
        mBinding.ivSkin.visibility = View.GONE
        if (mSiteLock) {
            if (!seatLockSkinUrl.isNullOrEmpty()) {
                Glide.with(context).load(seatLockSkinUrl).into(mBinding.ivSkin)
                mBinding.ivSkin.visibility = View.VISIBLE
            } else {
                mBinding.seatStateImg.setImageResource(R.drawable.party_seat_lock)
            }
        } else {
            if (!seatStateSkinUrl.isNullOrEmpty()) {
                Glide.with(context).load(seatStateSkinUrl).into(mBinding.ivSkin)
                mBinding.ivSkin.visibility = View.VISIBLE
            } else {
                mBinding.seatStateImg.setImageResource(R.drawable.ic_sofa)
            }
        }
    }

    private fun setMike(b: Boolean) {
        mBinding.mike.setImageResource(if (b) R.drawable.party_mike_open else R.drawable.party_mike_close)
    }

    private fun setNoUser() {
        mBinding.apply {
            seatIdRlp.visibility = GONE
            seatIdImg.visibility = GONE
            seatIdRl.visibility = GONE
            nameTv.text = StringUtils.getString(string.name_no, mSeatId + 1)
            nameTv.setTextColor(ColorUtils.getColor(R.color.white))
            giftTv.text = "0"
            avFv.setFrameImage(null)
            noUserRoundRl.visibility = VISIBLE
            avatarImg.visibility = GONE
            avatarImg.setImageResource(0)
            mike.setImageResource(0)
        }
    }

    private fun setName(userBasicVo: UserBasicVo) {
        mBinding.apply {
            seatIdRlp.visibility = VISIBLE
            if (userBasicVo.userId == mUserId) { //房主
                seatIdImg.visibility = VISIBLE
                seatIdRl.visibility = GONE
            } else {
                seatIdImg.visibility = GONE
                seatIdRl.visibility = VISIBLE
                seatIdTv.text = "${mSeatId + 1}"
            }
            nameTv.text = userBasicVo.nickname
            nameTv.setNicknameColor(
                userBasicVo.nicknameColor,
                R.color.white
            )
        }

    }

    /**
     * 设置头像和头像框
     */
    private fun setAvatar(userBasicVo: UserBasicVo) {
        mBinding.apply {
            noUserRoundRl.visibility = GONE
            avatarImg.visibility = VISIBLE
            avatarImg.setImageUrl(userBasicVo.avatar, userBasicVo.userId)
            avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(userBasicVo.avatarFrame))
        }
    }

    /**
     * 这个座位上得声浪变化
     */
    override fun SoundLevelUpdate(soundLevel: Float) {
        if (soundLevel > 2) {
            showPag()
        } else {
            pausePag()
        }
    }

    override fun upDataSoundPath(soundPath: String) {
        if (soundPath == PagConst.LIVE_PARTY_SEAT) {
            mBinding.pag.apply {
                path = soundPath
                setRepeatCount(-1)
            }
        } else {
            val pageFile = PAGFile.Load(soundPath)
            mBinding.pag.apply {
                setRepeatCount(-1)
                composition = pageFile
            }
        }
    }

    override fun showEmoji(message: SendLiveMessage) {
        mBinding.pagEmoji.playAsyncWithOwner(findViewTreeLifecycleOwner(), message.content)
    }

    override fun upDataUserPosition(position: Int) {
        //如果是0就是房主 不管
        if (mSeatId == 0) return
        mBinding.apply {
            when (position) {
                ADMINISTRATOR -> {
                    seatIdImg.visibility = VISIBLE
                    seatIdRl.visibility = GONE
                    seatIdImg.setImageResource(R.drawable.party_administrator_icon)
                }

                GENERAL_AUDIENCE -> {
                    seatIdImg.visibility = GONE
                    seatIdRl.visibility = VISIBLE
                }

                HOMEOWNER -> {
                    seatIdImg.visibility = VISIBLE
                    seatIdRl.visibility = GONE
                    seatIdImg.setImageResource(R.drawable.ic_rank_room)
                }
            }
        }
    }

    override fun upDateSkinView(resp: UserRoomThemeResp?) {
        if (resp != null) {
            seatStateSkinUrl = ImageUtils.imgPath(resp.prefix + resp.site)
            seatLockSkinUrl = ImageUtils.imgPath(resp.prefix + resp.siteLock)
        } else {
            seatStateSkinUrl = null
            seatLockSkinUrl = null
        }
        setLock()
    }

    private fun pausePag() {
        if (mBinding.pag.visibility != INVISIBLE) {
            mBinding.pag.visibility = INVISIBLE
        }
        if (mBinding.pag.isPlaying) {
            mBinding.pag.pause()
        }
    }

    private fun showPag() {
        if (mBinding.pag.visibility != VISIBLE) {
            mBinding.pag.visibility = VISIBLE
        }
        if (!mBinding.pag.isPlaying) {
            mBinding.pag.play()
        }
    }

    companion object {

        //用户职位
        const val ADMINISTRATOR = 1                 //管理员
        const val GENERAL_AUDIENCE = 2              //普通观众
        const val HOMEOWNER = 3                     //房主id
    }

}